package scripts;
import bot.ScriptMethods;
import java.util.Arrays;
import javax.swing.JOptionPane;

public class ARSThiever extends ScriptMethods implements Runnable {

	private int[] foodIDs = { 350, 352, 362, 364, 367, 370, 373, 546, 555, 551,
			553, 355, 359, 357, 326, 327, 328, 329, 330, 332, 334, 335, 336,
			337, 750, 346, 249, 257, 258, 259, 261, 262, 263, 210 };
	private int[] IDs = { 65 };
	private int eatAt;
	private int stage;

	public void start() {
		System.gc();

		getAccountSettings();

		String nids = JOptionPane.showInputDialog("Enter NPC IDs (multiple IDs separated with , no spaces): "); 
		if (!nids.equals("") || !nids.equals(null)) {
			IDs = stringToIntArray(nids);
		}

		String hp = JOptionPane.showInputDialog("Enter hits to eat at: "); 
		if (!hp.equals("") || !hp.equals(null)) {
			eatAt = Integer.parseInt(hp);
		} else {
			eatAt = 20;
		}

		System.out.println("\n");
		System.out.println("- Storm's ARSThiever -");
		System.out.println("\n");
		System.out.println("NPC IDs: " + Arrays.toString(IDs));
		System.out.println("We are eating at Hits: " + eatAt);
		System.out.println("\n");

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
		scriptThread = null;
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();
				if (inCombat()) {
					if (stage != 1) {
						changeStage(1);
					}
					walkTo(ourPlayerX(), ourPlayerY(), false);
					wait(610);
				}
				if (!inCombat() && getCurrentLevel()[3] <= eatAt) {
					if (stage != 2) {
						changeStage(2);
					}
					for (int i : foodIDs) {
						useItem(i);
						wait(random(600, 800));
					}
					wait(100);
				}
				if (!isSleeping() && !inCombat() && getCurrentLevel()[3] > eatAt) {
					if (stage != 3) {
						changeStage(3);
					}
					thieveNpc(IDs);
					wait(random(600, 700));
				}
				wait(50);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void changeStage(int s) throws Exception {
		if (s == 1) {
			debug("You are in combat, running!");
		}
		if (s == 2) {
			debug("You are low hits, we need to eat.");
		}
		if (s == 3) {
			debug("Thieving NPC");
		}
		stage = s;
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 90 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
}